<template>
    <div class="app-container">
        <el-row :gutter="24">
            <el-col :span="24" :xs="24">
                <el-form :model="queryParams" ref="queryRef" :inline="true" style="margin-left: 25px;">
                    <el-form-item label="分类名称" prop="name">
                        <el-input v-model="queryParams.name" placeholder="请输入" clearable style="width: 220px" maxlength="30"
                            @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item style="margin-left: 0px;">
                        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                        <el-button type="success" icon="Plus" @click="handleAdd">新建</el-button>
                    </el-form-item>
                </el-form>

                <el-table v-loading="loading" :data="detectionList"
                    style="margin-top: 30px;margin-left: 10px;margin-right: 10px;width: calc(100% - 20px);">
                    <el-table-column type="index" width="50" label="序号" />
                    <el-table-column label="分类" align="center" prop="name" />
                    <el-table-column label="创建人" align="center" prop="creatorName" />
                    <el-table-column label="创建时间" align="center" prop="createTime" />
                    <el-table-column label="备注" align="center" prop="remark" />
                    <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width"
                        width="120px">
                        <template #default="scope">
                            <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                            <el-button link type="danger" @click="handleDel(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                    @pagination="getList" style="margin-right: 20px;" />
            </el-col>
        </el-row>

        <el-dialog v-model="dialogVisible" draggable :title="dialogInfo.id ? '编辑' : '新增'" width="600px">
            <div v-loading="dialogloading">
                <el-form label-width="100px">
                    <el-form-item label="分类名称" prop="name" style="margin-left: 0px;">
                        <el-input v-model="dialogInfo.name" autocomplete="off" style="width: 400px" maxlength="50" />
                    </el-form-item>
                    <el-form-item label="备注" prop="remark" style="margin-left: 0px;">
                        <el-input v-model="dialogInfo.remark" autocomplete="off" style="width: 400px" maxlength="200" />
                    </el-form-item>
                </el-form>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="submitEdit">确认</el-button>
                </span>
            </div>
        </el-dialog>
    </div>
</template>
 
<script setup name="User">
import { apiGetSupplierKindList, apiAddSupplierKind, apiEditSupplierKind } from "@/api/supplier";
const { proxy } = getCurrentInstance();

const detectionList = ref([]);
const loading = ref(false);
const total = ref(0);
const dialogInfo = ref({})
const dialogVisible = ref(false)
const dialogloading = ref(false)

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
    },
    rules: {
    },
});

const { queryParams, form } = toRefs(data);

/** 查询列表 */
const getList = () => {
    loading.value = true;
    apiGetSupplierKindList(queryParams.value).then(res => {
        if (res && res.data) {
            detectionList.value = res.data.records;
            total.value = res.data.total;
        } else {
            detectionList.value = [];
            total.value = 0;
        }
        loading.value = false;
    }).catch(err => {
        loading.value = false;
        detectionList.value = [];
        total.value = 0;
    });
};

getList();

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef");
    handleQuery();
};

const handleAdd = () => {
    dialogInfo.value = {}
    dialogVisible.value = true
}

const handleEdit = (row) => {
    dialogInfo.value = row
    dialogVisible.value = true
};

const submitEdit = (row) => {
    if (row.id) {
        //编辑
        dialogloading.value = true;
        apiEditSupplierKind({
            "id": dialogInfo.value.id,
            "name": dialogInfo.value.name,
            "remark": dialogInfo.value.remark,
        }).then(res => {
            getList()
            proxy.$modal.msgSuccess("操作成功");
            dialogVisible.value = false
            dialogloading.value = false;
        }).catch(err => {
            dialogloading.value = false;
        });

    } else {
        // 新增
        dialogloading.value = true;
        apiAddSupplierKind({
            "name": dialogInfo.value.name,
            "remark": dialogInfo.value.remark,
        }).then(res => {
            getList()
            proxy.$modal.msgSuccess("操作成功");
            dialogloading.value = false;
            dialogVisible.value = false
        }).catch(err => {
            dialogloading.value = false;
        });
    }
};

const handleDel = (row) => {
    proxy.$modal.confirm('是否确认删除该数据项？').then(function () {
        return delUser(row.userId || '');
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("操作成功");
    }).catch(() => { });
};

</script>
 

<style lang="scss" scoped>
.dialog-item-title {
    color: #000;
    font-size: 17px;
    padding-left: 10px;
    margin: 0 20px;
    border-left: 4px #00BFFF solid;
}

.now-select-txt {
    font-size: 15px;
    color: #fff;
    background: #00BFFF;
    padding: 10px 20px;
    border-radius: 10px 10px 0 0;

    span {
        font-size: 20px;
        font-weight: bold;
        margin: 0 10px;
    }

    div {
        cursor: pointer;
        margin-left: 30px;
        font-weight: bold;
    }
}
</style>